const jwt = require("jsonwebtoken")
const {User} = require("../models")
const {BadRequest, NotFound, Unauthorized} = require("http-errors")

module.exports = async (req, res, next) => {
    try {
        const {token} = req.headers
        if(!token) {
            throw new Unauthorized("当前接口需要认证才能访问")
        }

        const decoded = jwt.verify(token, process.env.SECRET)

        const {userId} = decoded

        req.userId = userId
        next()
    } catch(err) {
        next(err)
    }
}